#include "../Common/Net.hpp"
#include "../Common/Message.hpp"
#include <thread>
#include "../Common/Dispatcher.hpp"
// using ConnectionCallback = std::function<void(const BaseConnection::ptr &conn)>;
// using MessageCallback = std::function<void(const BaseConnection::ptr &conn, const BaseMessage::ptr &buf)>;
// using CloseCallback = std::function<void(const BaseConnection::ptr &conn)>;

void OnRpcMessage(const zgwrpc::BaseConnection::ptr &conn, const zgwrpc::BaseMessage::ptr &buf)
{
    cout << "OnRpcMessage:" << buf->serialize() << endl;
}


int main()
{



    zgwrpc::BaseClient::ptr client = zgwrpc::ClientFactory::create(8888, "127.0.0.1");


    zgwrpc::Dispatcher::ptr dispatcher = zgwrpc::DispatcherFactory::create();
    dispatcher->registerHandler<zgwrpc::RpcResponse>(zgwrpc::MType::RSP_RPC, OnRpcMessage);
    client->setMessageCallback(std::bind(&zgwrpc::Dispatcher::dispatch, dispatcher, std::placeholders::_1, std::placeholders::_2));

    zgwrpc::RpcRequest::ptr req = zgwrpc::MessageFactory::create<zgwrpc::RpcRequest>();
    req->setId(zgwrpc::Uuid::Getid());
    req->setMethod("Add");
    Json::Value params;
    params["num1"]=90;
    params["num2"]=1;
    req->setParams(params);
    req->setMType(zgwrpc::MType::REQ_RPC);
    req->setRtype(zgwrpc::Rtype::REQ_ASYNC);

    client->send(req);

    std::this_thread::sleep_for(std::chrono::seconds(2));
    return 0;
}
